package com.syqc.monitor.popup

import android.content.Context
import android.os.Bundle
import androidx.annotation.StyleRes
import androidx.appcompat.app.AlertDialog
import androidx.core.view.isVisible
import com.syqc.comlib.utils.FileUtil
import com.syqc.entity.HistoryVideo
import com.syqc.monitor.R
import com.syqc.monitor.databinding.DialogDownloadBinding
import com.syqc.utils.ToolsUtil

class DownLoadDialog(
    context: Context,
    @StyleRes themeResId: Int = R.style.CustomDialog,
    private val onCancel: () -> Unit,
    private val onPlay: () -> Unit
) : AlertDialog(context, themeResId) {
    private lateinit var binding: DialogDownloadBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DialogDownloadBinding.inflate(layoutInflater)
        setContentView(binding.root)
        setCancelable(false)
        setCanceledOnTouchOutside(false)

        binding.tvDownState.setOnClickListener {
            onCancel.invoke()
        }
        binding.tvDownPlay.setOnClickListener {
            onPlay.invoke()
        }
        setOnDismissListener {
            ToolsUtil.stopAmin(binding.ivProgress)
        }
    }

    fun setMessage(msg: String) {
        binding.state.text = "${getString(R.string.desc_com_state_current)} : $msg"
    }

    fun setMessage(msgId: Int) {
        binding.state.text =
            "${getString(R.string.desc_com_state_current)} : ${context.getString(msgId)}"
    }

    fun setJdSize(jd: String, size: String) {
        binding.upJd.text = "${getString(R.string.desc_up_jd)} : $jd"
        binding.upSize.text = "${getString(R.string.desc_up_size)} : $size"
    }

    fun setDownInfo(
        result: HistoryVideo.Result,
        sDate: String,
        eDate: String,
        downType: String,
        isChange: Boolean
    ) {
        val channelName = if (result.channelName.isNullOrEmpty()) {
            context.getString(R.string.desc_video_pass) + result.channel
        } else {
            result.channelName
        }
        binding.channel.text = getString(R.string.desc_video_passes) + " : $channelName"
        binding.sdate.text = "${getString(R.string.desc_choose_start_time)} : $sDate"
        binding.edate.text = "${getString(R.string.desc_choose_end_time)} : $eDate"
        binding.fileSize.text =
            if (isChange) "${getString(R.string.desc_video_file_size)} : --" else
                "${getString(R.string.desc_video_file_size)} : ${FileUtil.formatFileSize(result.fileSize)}"
        binding.type.text = "${getString(R.string.desc_video_type_down)} : ${getType(downType)}"
    }


    fun setDownTitle(title: String) {
        binding.tvDownState.text = title
    }

    fun setDownTitle(titleId: Int) {
        binding.tvDownState.text = context.getString(titleId)
    }

    fun setPlayVideo(isShow: Boolean) {
        binding.tvDownPlay.isVisible = isShow
    }

    fun setProgress(isProgress: Boolean) {
        if (isProgress) {
            ToolsUtil.startAminDown(binding.ivProgress)
            binding.ivProgress.setImageResource(R.mipmap.ic_download_progress)
        } else {
            ToolsUtil.stopAmin(binding.ivProgress)
            binding.ivProgress.setImageResource(R.mipmap.ic_download_dialog)
        }
    }

    fun getString(resId: Int): String {
        return context.getString(resId)
    }


    fun setVisible(isVisible: Boolean) {
        binding.upJd.isVisible = isVisible
        binding.upSize.isVisible = isVisible
    }

    private val types by lazy {
        context.resources.getStringArray(R.array.spinner_video)
    }

    private fun getType(key: String): String {
        return when (key) {
            "mp4" -> types[1]
            "flv" -> types[2]
            "mpegts" -> types[3]
            else -> types[0]
        }
    }
}